package com.AEWMS.Servlet;

import com.AEWMS.Dao.Parent;
import com.AEWMS.Pojo.StudentInfoView;
import com.AEWMS.Service.ParentService;
import com.AEWMS.Service.StudentService;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ParentRegisterServlet", urlPatterns = "/ParentRegisterServlet")
public class ParentRegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ParentName = request.getParameter("p1");
        String password = request.getParameter("p3");
        String RealName = request.getParameter("p4");
        String sex = request.getParameter("p5");
        String tel = request.getParameter("p6");
        String childStudentNumber = request.getParameter("p7");
        StudentService ss = new StudentService();
        StudentInfoView s = ss.findStuById(childStudentNumber);
        if(s.getStudentNumber()==null){
            response.getWriter().println(JSON.toJSONString("该学生不存在"));
        }else {
            ParentService ps = new ParentService();
            Parent p = ps.findChildById(childStudentNumber);
            if(p.getStudentNumber()!=null){
                response.getWriter().println(JSON.toJSONString("重复注册，您可直接使用孩子学号登录"));
                ps.closeCon();
                return;
            }else {
                ParentService pss = new ParentService();
                Parent r = new Parent();
                r.setParentName(ParentName);
                r.setParentPassword(password);
                r.setParentRealName(RealName);
                r.setParentSex(sex);
                r.setParentTel(tel);
                r.setStudentNumber(childStudentNumber);
                if (pss.insertPa(r)) {
                    response.getWriter().println(JSON.toJSONString("注册成功，可直接使用孩子学号登录"));
                    return;
                }
            }
        }

        ss.closeCon();



    }
}
